package com.lcy.redismessage.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @创建时间:
 * @描述 Redisson-redis锁配置类
 */
@Configuration
public class RedissonConfig {


    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private String database;


    @Bean
    public RedissonClient redisson() throws IOException {

        // 构造redisson实现分布式锁必要的Config
        Config config = new Config();
        String ht = "redis://" + host + ":" + port;
        config.useSingleServer().setAddress(ht).setDatabase(Integer.parseInt(database));
        // 构造RedissonClient
        RedissonClient redissonClient = Redisson.create(config);

        return redissonClient;
    }
}
